package com.youmu.sichuansfc.service.impl;

import com.youmu.sichuansfc.dao.FundManagerDao;
import com.youmu.sichuansfc.dao.FundManagerFundDao;
import com.youmu.sichuansfc.dao.FundManagerFundInvestDao;
import com.youmu.sichuansfc.dao.FundManagerStaffDao;
import com.youmu.sichuansfc.domain.*;
import com.youmu.sichuansfc.domain.pojo.SearchYoumuFundManagerFund;
import com.youmu.sichuansfc.dto.PageT;
import com.youmu.sichuansfc.dto.fund.*;
import com.youmu.sichuansfc.service.FundManagerFundInvestService;
import com.youmu.sichuansfc.service.FundManagerPartnerService;
import com.youmu.sichuansfc.service.FundManagerService;
import org.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class FundManagerServiceImpl implements FundManagerService {


    @Autowired
    FundManagerDao fundManagerDao;
    @Autowired
    FundManagerStaffDao fundManagerStaffDao;
    @Autowired
    FundManagerFundDao fundManagerFundDao;
    @Autowired
    FundManagerPartnerService fundManagerPartnerService;
    @Autowired
    FundManagerFundInvestService fundManagerFundInvestService;
    @Autowired
    FundManagerFundInvestDao fundManagerFundInvestDao;

    @Override
    public PageT findBy(Integer status, String fundName, int num, int size) {

        PageT pageT = fundManagerDao.findBy(status, fundName, num, size);

        return pageT;
    }


    @Override
    public YoumuFundManager uploadFundManager(FundManagerDto fundManagerDto, long fundUserId) throws Exception {

        // 基金管理人信息
        YoumuFundManager youmuFundManager = new YoumuFundManager();
        BeanUtils.copyProperties(fundManagerDto, youmuFundManager);

        // 新增
        youmuFundManager.setId(null);
        youmuFundManager.setAddTime(new Timestamp(new Date().getTime()));
        youmuFundManager.setIsDelete(false);
        youmuFundManager.setFundUserId(fundUserId);
        youmuFundManager.setStatus(0);
        youmuFundManager = fundManagerDao.upload(youmuFundManager);

        // 修改 历史
        if(fundManagerDto.getId() != null){
            YoumuFundManager youmuFundManagerChild = checkFundUserManager(fundUserId, fundManagerDto.getId());
            if ( null == youmuFundManagerChild){
                throw new Exception("ID 不 存在");
            }
            youmuFundManagerChild.setParentId(youmuFundManager.getId());
            youmuFundManager = fundManagerDao.upload(youmuFundManager);
        }


        // 基金股东信息
        List<FundManagerPartnerDto> fundManagerPartnerDtos = fundManagerDto.getPartnerList();
        if (fundManagerPartnerDtos != null && fundManagerPartnerDtos.size() > 0) {
            for (FundManagerPartnerDto fundManagerPartnerDto : fundManagerPartnerDtos) {
                YoumuFundManagerPartner youmuFundManagerPartner = new YoumuFundManagerPartner();
                BeanUtils.copyProperties(fundManagerPartnerDto, youmuFundManagerPartner);

                youmuFundManagerPartner.setFundManagerId(youmuFundManager.getId());
                youmuFundManagerPartner.setAddTime(new Date());

                fundManagerPartnerService.upload(youmuFundManagerPartner);
            }
        }


        // 基金管理人高管信息
        List<FundManagerStaffDto> fundManagerDtoStaffDtoList = fundManagerDto.getStaffDtoList();
        if (fundManagerDtoStaffDtoList != null && fundManagerDtoStaffDtoList.size() > 0) {
            for (FundManagerStaffDto fundManagerStaffDto : fundManagerDtoStaffDtoList) {
                YoumuFundManagerStaff youmuFundManagerStaff = new YoumuFundManagerStaff();
                BeanUtils.copyProperties(fundManagerStaffDto, youmuFundManagerStaff);

                youmuFundManagerStaff.setFundManagerId(youmuFundManager.getId());
                youmuFundManagerStaff.setAddTime(new Timestamp(new Date().getTime()));

                fundManagerStaffDao.upload(youmuFundManagerStaff);
            }
        }

        return youmuFundManager;
    }

    @Override
    public YoumuFundManager findByManagerName(String name) {
        return fundManagerDao.findByName(name);
    }

    @Override
    public List<FundManagerDto> findByFundUserId(long fundUserId) {
        List<FundManagerDto> fundManagerDtos = new ArrayList<FundManagerDto>();

        List<YoumuFundManager> youmuFundManageList = fundManagerDao.findByFundUserId(fundUserId);
        if(youmuFundManageList != null && youmuFundManageList.size() > 0){
            for (YoumuFundManager youmuFundManager : youmuFundManageList){
                FundManagerDto fundManagerDto = new FundManagerDto();

                BeanUtils.copyProperties(youmuFundManager, fundManagerDto);
                List<YoumuFundManagerStaff> youmuFundManagerStaffList = fundManagerStaffDao.findByFundManagerId(youmuFundManager.getId());

                // 查询该基金管理人下 高管信息
                List<FundManagerStaffDto> fundManagerStaffDtoList = new ArrayList<FundManagerStaffDto>();
                if (youmuFundManagerStaffList != null && youmuFundManagerStaffList.size() > 0){
                    for(YoumuFundManagerStaff youmuFundManagerStaff : youmuFundManagerStaffList){
                        FundManagerStaffDto fundManagerStaffDto = new FundManagerStaffDto();

                        BeanUtils.copyProperties(youmuFundManagerStaff, fundManagerStaffDto);
                        fundManagerStaffDtoList.add(fundManagerStaffDto);
                    }
                }
                fundManagerDto.setStaffDtoList(fundManagerStaffDtoList);
                fundManagerDtos.add(fundManagerDto);
            }
        }
        return fundManagerDtos;
    }

    @Override
    public FundManagerDto findById(long fundUserId, Long fundManagerId) {

        List<YoumuFundManager> youmuFundManageList = fundManagerDao.findByFundUserId(fundUserId);
        if(youmuFundManageList != null && youmuFundManageList.size() > 0){
            for (YoumuFundManager youmuFundManager : youmuFundManageList){
                if (youmuFundManager.getId().longValue() == fundManagerId){
                    FundManagerDto fundManagerDto = new FundManagerDto();

                    BeanUtils.copyProperties(youmuFundManager, fundManagerDto);
                    List<YoumuFundManagerStaff> youmuFundManagerStaffList = fundManagerStaffDao.findByFundManagerId(youmuFundManager.getId());

                    // 查询该基金管理人下 高管信息
                    List<FundManagerStaffDto> fundManagerStaffDtoList = new ArrayList<FundManagerStaffDto>();
                    if (youmuFundManagerStaffList != null && youmuFundManagerStaffList.size() > 0){
                        for(YoumuFundManagerStaff youmuFundManagerStaff : youmuFundManagerStaffList){
                            FundManagerStaffDto fundManagerStaffDto = new FundManagerStaffDto();

                            BeanUtils.copyProperties(youmuFundManagerStaff, fundManagerStaffDto);
                            fundManagerStaffDtoList.add(fundManagerStaffDto);
                        }
                    }
                    fundManagerDto.setStaffDtoList(fundManagerStaffDtoList);

                    // 公司股东 信息
                    String phql = "from YoumuFundManagerPartner where is_delete = 0 and fund_manager_id = :fundManagerId";
                    List<YoumuFundManagerPartner> youmuFundManagerPartnerList =fundManagerPartnerService.findByManagerId(youmuFundManager.getId())
                    fundManagerDto.setPartnerList(youmuFundManagerPartnerList);

                    return  fundManagerDto;
                }

            }
        }
        return null;
    }

    @Override
    public YoumuFundManager checkFundUserManager(Long fundUserId, Long fundManagerId) {
        List<YoumuFundManager> youmuFundManagerList = fundManagerDao.findByFundUserId(fundUserId);
        if (youmuFundManagerList != null && youmuFundManagerList.size() > 0 ){
            for (YoumuFundManager youmuFundManager : youmuFundManagerList){
                if (fundManagerId != null &&
                        youmuFundManager.getId().longValue() == fundManagerId.longValue()){
                    return  youmuFundManager;
                }
            }
            return youmuFundManagerList.get(youmuFundManagerList.size()-1);
        }
        return null;
    }

    @Override
    public YoumuFundManagerFund uploadFund(Long fundUserId, FundManagerFundDto fundManagerFundDto) throws Exception {
        YoumuFundManagerFund youmuFundManagerFund = new YoumuFundManagerFund();

        BeanUtils.copyProperties(fundManagerFundDto, youmuFundManagerFund);

        // 新增
        youmuFundManagerFund.setId(null);
        youmuFundManagerFund.setAddTime(new Timestamp(new Date().getTime()));
        youmuFundManagerFund.setFundUserId(fundUserId);
        youmuFundManagerFund.setStatus(0);
        youmuFundManagerFund.setIsDelete(false);
        System.out.println(new JSONObject(youmuFundManagerFund));

        youmuFundManagerFund = fundManagerFundDao.upload(youmuFundManagerFund);

        // 修改历史
        if(null != fundManagerFundDto.getId()){
            YoumuFundManagerFund youmuFundManagerFundchild = fundManagerFundDao.findById(fundManagerFundDto.getId());
            if(null == youmuFundManagerFund){
                throw  new Exception("改ID不存在");
            }
            youmuFundManagerFundchild.setParentId(youmuFundManagerFund.getId());
            fundManagerFundDao.upload(youmuFundManagerFundchild);
        }

        // 添加主要投向
        List<FundManagerFundInvestDto> fundManagerDtoStaffDtoList = fundManagerFundDto.getFundInvestDtos();
        if (fundManagerDtoStaffDtoList != null && fundManagerDtoStaffDtoList.size() > 0) {
            for (FundManagerFundInvestDto fundManagerStaffDto : fundManagerDtoStaffDtoList) {
                YoumuFundManagerFundInvest youmuFundManagerFundInvest = new YoumuFundManagerFundInvest();
                BeanUtils.copyProperties(fundManagerStaffDto, youmuFundManagerFundInvest);

                youmuFundManagerFundInvest.setManagerFundId(youmuFundManagerFund.getId());
                youmuFundManagerFundInvest.setAddTime(new Timestamp(new Date().getTime()));
                fundManagerFundInvestService.upload(youmuFundManagerFundInvest);
            }
        }

        return  youmuFundManagerFund;
    }

    @Override
    public YoumuFundManagerFund findFund(YoumuFundUser youmuFundUser, Long fundManagerFundId) {
         YoumuFundManagerFund   youmuFundManagerFund = fundManagerFundDao.fundUserfindfund(youmuFundUser.getId(), fundManagerFundId);
         youmuFundManagerFund.setFundInvestDtos(fundManagerFundInvestDao.findByManagerId(youmuFundManagerFund.getId()));

        return youmuFundManagerFund;
    }

    @Override
    public PageT funManagers(Integer investType, String fundManagerName, int num, int size) {
        return fundManagerDao.funmanagers(investType, fundManagerName, num, size);
    }

    public FundManagerDto findmanagerById(Long fundManagerId) {
        return  fundManagerDao.findById(fundManagerId);
    }

    @Override
    public PageT searchFund(Integer fundType, String searchName, int num, int size) {
        return fundManagerFundDao.findBy(fundType, searchName, num, size);
    }

    @Override
    public SearchYoumuFundManagerFund findFundById(Long fundId) {
        SearchYoumuFundManagerFund searchYoumuFundManagerFund =  fundManagerFundDao.findById2(fundId);

        searchYoumuFundManagerFund.setFundInvestDtos(fundManagerFundInvestDao.findByManagerId(searchYoumuFundManagerFund.getId()));

        return searchYoumuFundManagerFund;
    }

    @Override
    public List<YoumuFundManagerFund> findByManagerId(Long managerId) {
        return fundManagerFundDao.findByManagerId(managerId);
    }

//    @Override
//    public SearchYoumuFundManagerFund findById(Long fundId) {
//
//    }
    @Override
    public List<YoumuFundManager> findByTime(Calendar startTime, Calendar endTime) {
        return fundManagerDao.findByTime(startTime, endTime);
    }

    @Override
    public SearchYoumuFundManagerFund findFundByName(String name) {
        return fundManagerFundDao.findByFundName(name);
    }

    @Override
    public PageT funManagersByUser(YoumuFundUser youmuFundUser, int num, int size) {
        return fundManagerDao.findByFundUser(youmuFundUser.getId(), num , size);
    }

    @Override
    public PageT findFundListByFundUserId(Long fundUserId, int num, int size) {
        return fundManagerFundDao.fundListByfundUserId(fundUserId, num , size);
    }

    @Override
    public PageT findHistoryByFundUserId(Long fundUserId, int num, int size, Long fundId) {
        return fundManagerFundDao.fundHistoryByfundId(fundUserId, num , size, fundId);;
    }
}
